package com.vehicle.service.imp;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.vehicle.bean.DataGridView;
import com.vehicle.mapper.CarMapper;
import com.vehicle.pojo.Car;
import com.vehicle.service.CarService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class CarServiceImpl implements CarService {

    @Autowired
    private CarMapper carMapper;

    public DataGridView findCarByPage(Car car) {
        Page<Object> page = PageHelper.startPage(car.getPage().intValue(), car.getLimit().intValue());
        List<Car> cars = this.carMapper.selectCarByPage(car);
        DataGridView dataGridView = new DataGridView();
        dataGridView.setCount(page.getTotal());
        dataGridView.setData(cars);
        return dataGridView;
    }

    //查询所有汽车状态
    @Override
    public DataGridView findCarStatus(Car car) {
        Page<Object> page = PageHelper.startPage(car.getPage(),car.getLimit());
        List<Car> carList =  carMapper.CarStatusFindAll(car);
        DataGridView dataGridView = new DataGridView();
        dataGridView.setCount(page.getTotal());
        dataGridView.setData(carList);
        return dataGridView;
    }


    @Override
    public List<Car> findAllCar() {
        return carMapper.selectAllCar();
    }
    //根据Carid查询Car
    @Override
    public Car findCarByCarid(Integer carid) {
        return carMapper.selectCarByCarid(carid);
    }

    //增加
    @Override
    public void updateCarable(Integer carid, Integer status) {
        carMapper.updateCarableById(carid,status);
    }

    @Override
    public void addCar(Car car) {
        carMapper.insertCar(car);
    }

    @Override
    public void updateCarStartKM(Integer carid, Integer startkm) {
        carMapper.updateCarStartKM(carid,startkm);
    }

    @Override
    public void editCar(Car car) {
        carMapper.updateCar(car);
    }

    //单个删除
    @Override
    public void updateStatus(Integer carid) {
        carMapper.updateStatusToZero(carid);
    }

    //批量删除
    @Override
    public void updateBatchStatus(Integer[] carids) {
        if (carids!=null&&carids.length>0){
            for (Integer carid:carids
                 ) {
                carMapper.updateStatusToZero(carid);
            }
        }
    }
}